﻿using Dapper;
using MyDapperApi.Models;
using MyDapperApi.Repositories.Interfaces;
using System.Data;

namespace MyDapperApi.Repositories.Implementations;

public class UserRepository : RepositoryBase<User>, IUserRepository
{
    public UserRepository(IConfiguration configuration)
        : base(configuration, "Users") // 指定表名
    {
    }

    public async Task<User?> GetUserByEmailAsync(string email)
    {
        using var connection = CreateConnection();
        var sql = "SELECT * FROM Users WHERE Email = @Email";
        return await connection.QueryFirstOrDefaultAsync<User>(sql, new { Email = email });
    }

    public async Task<IEnumerable<User>> GetUsersByAgeRangeAsync(int minAge, int maxAge)
    {
        using var connection = CreateConnection();
        var sql = "SELECT * FROM Users WHERE Age BETWEEN @MinAge AND @MaxAge";
        return await connection.QueryAsync<User>(sql, new { MinAge = minAge, MaxAge = maxAge });
    }

    // 如果需要，可以重写基类方法
    protected override string GetPrimaryKeyName() => "Id"; // 明确指定主键名
}